/*
 * Name: Rag
 * Author: Sam Oakley
 * Date: 29/3/2010
 * Version Number: 1.1
 * Description: Starting point for users. Just provides a list view for navigation, really.
 * Pressing 'menu' lets you change update freq.
 * Changes History: 
 * 1.0 Created project
 * 1.1 Updated 'about'
 */
package com.blork.rag;


import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Rag extends ListActivity {
	public static final String TAG = "Sheffield RAG";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);	
        
        PackageManager pm = getPackageManager();
        
		try {
			PackageInfo pi = pm.getPackageInfo("com.blork.rag", PackageManager.GET_ACTIVITIES);
			int version = pi.versionCode;
			
	        int storedVersion = settings.getInt("version", 1);
	        if(storedVersion != version){
	        	SharedPreferences.Editor vEditor = settings.edit();
	        	vEditor.putInt("version", version);
	            vEditor.commit();
	            
	    		Intent intent = new Intent(this, About.class);
	    		startActivity(intent); 
	        }
	        
		} catch (NameNotFoundException e1) {
			e1.printStackTrace();
		}
		
        
        this.startService(new Intent(this, EventService.class));
        
        String[] options = getResources().getStringArray(R.array.main_options_array);
        
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, options));

        ListView lv = getListView();
       
        lv.setTextFilterEnabled(true);
               
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            	onClick(position);
            }
         });
        
    }
    
    public void onResume(){
		super.onResume();
    	
		  try {
			  	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
			  	
				String notificationFreq = settings.getString("notification_freq", "Every hour");
	           
				AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
	            Intent intent = new Intent(Rag.this, EventService.class);
	            PendingIntent sender = PendingIntent.getBroadcast(Rag.this,
	                    0, intent, 0);
	            
				//If they've turned updates off, stop them, else begin checking
				if(notificationFreq.equals("Never")){
					Log.i(Rag.TAG, "Cancelling updates");
					 am.cancel(sender);
				}else{
					Long repeat = AlarmManager.INTERVAL_HOUR; 
					if(notificationFreq.equals("Once a day")){
						repeat = AlarmManager.INTERVAL_DAY;
					}else if(notificationFreq.equals("Once a week")){
						repeat = 7*AlarmManager.INTERVAL_DAY;
					}

		            // Schedule the alarm!
		            long firstTime = SystemClock.elapsedRealtime();
		            
		            am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                            firstTime+repeat, repeat, sender);
		            
		            Log.i(Rag.TAG, "Set updates for "+notificationFreq);
				}
	

	            
			} catch (Exception e) { 
				Log.e("",e.toString());
			}
    }

    /* Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
	    	Intent e = new Intent(this, Settings.class);
			startActivity(e);
        return true;
    }     
    
	public void onClick(int position) {
		switch (position){
			case 0: //upcoming events
				Intent a = new Intent(this, Upcoming.class);
				startActivity(a);
			break;
//			case 1:	//your profile
//				Intent b = new Intent(this, Profile.class);
//				startActivity(b);
//			break;
			case 1:	//contact us
				Intent c = new Intent(this, Contact.class);
				startActivity(c);
			break;
			case 2: //about
				Intent d = new Intent(this, About.class);
				startActivity(d);
			break;
		}
	}
		
}
